﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Biblioteca.Utils;
using Biblioteca.Objetos;
using SEG.Objetos;
using SEG.Utils;
using Pms.Utils;
using System.Collections.Generic;

namespace SEG
{
    public partial class GrupoInterfacesManutencao : System.Web.UI.Page
    {
        private Permissao permissao;
        private string cod_grp = String.Empty;
        private string cod_aplic = String.Empty;
        Usuario usuAut;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session.Count == 0)
            {
                Response.Redirect("~/Login.aspx");
            }
            else
            {
                usuAut = (Usuario)Session["usuario"];
                char perm = BiblioPMS.VerifiquePermissao("GRUPOSINTERFACEMANUT.ASPX", BiblioPMS.aplicacaoSEG, usuAut.Login);
                if (!perm.Equals('M'))
                    Response.Redirect("Inicio.aspx");
            }

            if (Request["codGrp"] != null)
                cod_grp = Server.UrlDecode(Criptografia.Descriptografar(Request["codGrp"]));




            if (!Page.IsPostBack)
            {
                try
                {
                    Grupo grp = Grupo.Pesquisar(usuAut.Login, Criptografia.Descriptografar(Request["codGrp"]));
                    lblTitulo.Text = lblTitulo.Text + " do Grupo " + grp.Nom_grp;
                }
                catch (Exception ex)
                {
                    lblErro.Text = "*Erro: " + ex.Message;
                    lblErro.Visible = true;                   
                }

                AdicionarAplicacao();

                if (Request["codAplic"] != null)
                {
                    cod_aplic = Server.UrlDecode(Criptografia.Descriptografar(Request["codAplic"]));
                    ddlAplicacao.SelectedValue = cod_aplic;
                }

                hplVoltar.NavigateUrl = "GruposInterfaces.aspx?codGrp=" + Server.UrlEncode(Criptografia.Criptografar(cod_grp));


                CarregarListInterface();

                CarregarListInterfaceGrupo();
            }          
        }

        private void AdicionarAplicacao()
        {
            try
            {
                ListItem item = new ListItem("Selecione a Aplicação...", "0");
                ddlAplicacao.Items.Add(item);

                List<Aplicacao> ListAplicacao = Aplicacao.PesquisarAplicacoes(usuAut.Login);
                
                if (ListAplicacao != null)
                {
                    foreach (Aplicacao objAplic in ListAplicacao)
                    {
                        item = new ListItem(objAplic.Dsc_aplic,objAplic.Cod_aplic);
                        ddlAplicacao.Items.Add(item);       
                    }
                }
            }
            catch (Exception ex)
            {
                lblErro.Visible = true;
                lblErro.Text = "*Erro: " + ex.Message;
            }
        }


        private void CarregarListInterfaceGrupo()
        {
            try
            {
                lsbInterGrupo.Items.Clear();
                ListItem item;
                List<Interface> ListInter = Interface.PesquisarInterfaceComPermissao(usuAut.Login, cod_grp, ddlAplicacao.SelectedValue);
                if (ListInter != null)
                {
                    foreach (Interface objInter in ListInter)
                    {
                        item = new ListItem(objInter.Dsc_aplic, objInter.Cod_int);
                        lsbInterGrupo.Items.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {
                lblErro.Visible = true;
                lblErro.Text = "*Erro: " + ex.Message;
            }
     
        }

        private void CarregarListInterface()
        {
            try
            {
                lsbInter.Items.Clear();                
                ListItem item;
                List<Interface> ListInter = Interface.PesquisarInterfaceSemPermissao(usuAut.Login, cod_grp, ddlAplicacao.SelectedValue);
                if (ListInter != null)
                {
                    foreach (Interface objInter in ListInter)
                    {
                        item = new ListItem(objInter.Dsc_aplic,objInter.Cod_int);
                        lsbInter.Items.Add(item);                    
                    }
                }
            }
            catch (Exception ex)
            {
                lblErro.Visible = true;
                lblErro.Text = "*Erro: " + ex.Message;
            }
        }

        protected void chkDesativar_CheckedChanged(object sender, EventArgs e)
        {
            if (chkDesativar.Checked.Equals(true))
            {
                lsbInterGrupo.SelectionMode = ListSelectionMode.Single;
                ddlConsulta.Enabled = false;
                ddlManut.Enabled = false;          
            }
            else
            {
                lsbInterGrupo.SelectionMode = ListSelectionMode.Multiple;
                ddlConsulta.Enabled = true;
                ddlManut.Enabled = true;
            }
        }

        protected void btnInserir_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (ListItem item in lsbInter.Items)
                {
                    if (item.Selected)
                    {
                        permissao = new Permissao(cod_grp, item.Value, ddlConsulta.SelectedValue, ddlManut.SelectedValue);
                        permissao.incluirPermissao(usuAut.Login);
                    }                    
                }

                CarregarListInterface();
                CarregarListInterfaceGrupo();
            }
            catch (Exception ex)
            {
                lblErro.Visible = true;
                lblErro.Text = "*Erro: " + ex.Message;
            }

        }

        protected void btnRemover_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (ListItem item in lsbInterGrupo.Items)
                {
                    if (item.Selected)
                    {
                        permissao = new Permissao(cod_grp, item.Value, ddlConsulta.SelectedValue, ddlManut.SelectedValue);
                        permissao.excluirPermissao(usuAut.Login);
                    }
                }
                
                CarregarListInterface();
                CarregarListInterfaceGrupo();
            }
            catch (Exception ex)
            {
                lblErro.Visible = true;
                lblErro.Text = "*Erro: " + ex.Message;
            }
        }

        protected void ddlAplicacao_SelectedIndexChanged(object sender, EventArgs e)
        {    
            try
            {
                CarregarListInterface();
                CarregarListInterfaceGrupo();
            }
            catch (Exception ex)
            {
                lblErro.Visible = true;
                lblErro.Text = "*Erro: " + ex.Message;
            }
        }

    }
}
